package ar.com.mindset.geometrics.effects {
	import ar.com.mindset.geometrics.MapItem;
	import ar.com.mindset.geometrics.RegionsEffectStrategy;

	import mx.effects.AnimateProperty;
	import mx.effects.CompositeEffect;
	import mx.effects.Effect;
	import mx.effects.Parallel;
	import mx.effects.Sequence;

	public class ImplodeEffectStrategy implements RegionsEffectStrategy {

		public function createEffect(regions:Array):Effect {
			var retVal:CompositeEffect=new Sequence();

			var effectPrepare:CompositeEffect=new Parallel();
			var effectIn:CompositeEffect=new Parallel();
			for (var i:Number=0; i < regions.length; i++) {
				var region:MapItem=regions[i];



				effectPrepare.addChild(getPrepareEffect(region));
				effectIn.addChild(getEffectIn(region));
			}
			effectIn.duration=2000;

			retVal.addChild(effectPrepare);
			retVal.addChild(effectIn);

			return retVal;
		}

		private function getEffectIn(target:Object):Effect {

			var retVal:Parallel=new Parallel();
			var movX:AnimateProperty=new AnimateProperty();
			movX.property="x";
			movX.toValue=0;
			var movY:AnimateProperty=new AnimateProperty();
			movY.property="y";
			movY.toValue=0;

			retVal.addChild(movX);
			retVal.addChild(movY);

			retVal.target=target;
			return retVal;
		}

		private function getPrepareEffect(target:Object):Effect {
			var retVal:Parallel=new Parallel();

			var prepX:AnimateProperty=new AnimateProperty();
			prepX.property="x";
			var prepY:AnimateProperty=new AnimateProperty();
			prepY.property="y";
			var prepAlpha:AnimateProperty=new AnimateProperty();
			prepAlpha.property="alpha";
			var prepVisible:AnimateProperty=new AnimateProperty();
			prepVisible.property="visible";
			var prepScaleX:AnimateProperty=new AnimateProperty();
			prepScaleX.property="scaleX";
			var prepScaleY:AnimateProperty=new AnimateProperty();
			prepScaleY.property="scaleY";					

			if (Math.random() < 0.5) {
				//left
				prepX.toValue=-800;
			} else {
				//right
				prepX.toValue=3000
			}
			prepY.toValue=Math.random() * 2000;
			prepVisible.toValue=1;
			prepAlpha.toValue=1;
			prepScaleX.toValue=1;
			prepScaleY.toValue=1;

			retVal.duration=0;

			retVal.addChild(prepAlpha);
			retVal.addChild(prepVisible);
			retVal.addChild(prepX);
			retVal.addChild(prepY);
			retVal.addChild(prepScaleX);
			retVal.addChild(prepScaleY);

			retVal.target=target;
			return retVal;
		}

		public function toString():String {
			return "Implode";
		}

	}
}